package com.mm.gbicc.dao.conn;

/**
 * MySql 简单的数据连接池
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class OracleConn implements IJdbcDb {
	private static OracleConn instance = null;
	private List freeConns;// 空闲队列List<Connection>

	public static String maxConn = "100";// 最大连接数
	public static String minFreeConn = "10";// 最小连接数（连接池初始连接数）
	String driver = null;
	String url = null;
	String user = null;
	String password = null;
	private int maxNum = 0;// 最大连接数
	private int minFreeNum = 0;// 最小连接数
	private int curBusyNum = 0;// 当前连接数
	private int curFreeNum = 0;// 当前空闲连接数

	public static String overInfo = "数据库连接数已达最大值，请稍后再试！";

	/**
	 * 只能取唯一实例
	 */
	public static synchronized OracleConn getInstance() {
		if (instance == null) {
			try {
				instance = new OracleConn();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return instance;
	}

	private OracleConn() {
		freeConns = new ArrayList();
		ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
		
		maxNum = 100;
		minFreeNum = 10;
		driver = bundle.getString("jdbc.driverClassName");;
		url = bundle.getString("jdbc.url");
		user = bundle.getString("jdbc.username");
		password = bundle.getString("jdbc.password");

		
		
	}

	/**
	 * 取得数据库连接
	 */
	public synchronized Connection getConn() {
		Connection conn = null;
		if (curFreeNum > 0) {
			conn = (Connection) freeConns.get(0);
			freeConns.remove(0);
			curFreeNum--;
			curBusyNum++;
		} else if (curBusyNum < maxNum) {
			conn = newConn();
			if (conn != null) {
				curBusyNum++;
			} else {
				// 报错，无法取得新的数据库连接
			}
		} else {
			// 数据库连接数已达到最大值
			System.out.println(overInfo);
		}
		return conn;
	}

	/**
	 * 断开数据库连接
	 */
	public void freeConn(Connection conn) {
		if (curFreeNum + curBusyNum >= maxNum) {
			// 不会有这种情况，在申请连接的时候就限制了。
		} else {
			freeConns.add(conn);
			curFreeNum++;
			curBusyNum--;
		}
	}

	/**
	 * 新建立一个连接
	 * 
	 * @return
	 */
	public Connection newConn() {
		Connection conn = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	/**
	 * 释放所有的连接
	 */
	public void freeAllConn() {
		try {
			while (freeConns.size() > 0) {
				Connection conn = (Connection) freeConns.get(0);
				freeConns.remove(0);
				if (conn != null)
					conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void displayPool() {
		 System.out.println("DB dirver is : "+driver);
		 System.out.println("DB url is : "+url);
		 System.out.println("DB user is : "+user);
		 System.out.println("DB password is : "+password);
		 System.out.println("DB maxNum is : "+maxNum);
		 System.out.println("DB minFreeNum is : "+minFreeNum);
		 System.out.println("DB curBusyNum is : "+curBusyNum);
		 System.out.println("DB curFreeNum is : "+curFreeNum);
		 System.out.println("DB current connpool size is : "+freeConns.size());
	}

	public static void main(String[] args) throws Exception {
		OracleConn ss = OracleConn.getInstance();
		ss.displayPool();
		System.out.println(ss.getConn());
	}
}
